package com.dbuil.bleshoes.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dbuil.bleshoes.R;
import com.dbuil.bleshoes.base.BaseActivity;
import com.dbuil.bleshoes.ui.fragment.HomeFragment;
import com.dbuil.bleshoes.ui.fragment.LifeFragment;
import com.dbuil.bleshoes.ui.fragment.MineFragment;
import com.dbuil.bleshoes.ui.fragment.RunFragment;
import com.dbuil.bleshoes.ui.fragment.ShopFragment;
import com.dbuil.bleshoes.ui.view.MyFragmentTabHost;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.OnClick;

public class HomeActivity2 extends BaseActivity {
    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.tabHost)
    MyFragmentTabHost tabHost;
    @BindArray(R.array.main_sorts)
    String[] tabs;

    private Class[] mFragmentClasses = new Class[]{HomeFragment.class, LifeFragment.class, RunFragment.class,
            ShopFragment.class, MineFragment.class};
    private int[] selectorImg = new int[]{R.drawable.shouye_selector, R.drawable.shouye_selector, R.drawable.shouye_selector,
            R.drawable.shouye_selector, R.drawable.shouye_selector};
    private String currentpage;

    @Override
    public int getLayoutId() {
        return R.layout.activity_home2;
    }

    @Override
    public void initView() {
        super.initView();

        currentpage = tabs[0];
        // 初始化tabHost
        tabHost.setup(this, getSupportFragmentManager(), R.id.fl_content);
        tabHost.getTabWidget().setShowDividers(0);//去掉分割线

        for (int i = 0; i < tabs.length; i++) {
            tabHost.addTab(tabHost.newTabSpec(tabs[i]).setIndicator(getTabView(i)), mFragmentClasses[i], null);
        }

        // 设置默认tab
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                currentpage = tabId;
            }
        });


    }


    @Override
    public void initData() {
        super.initData();
    }


    private View getTabView(int index) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
        TextView tv_indicator = inflate.findViewById(R.id.tv_indicator);
        RelativeLayout rlTab = inflate.findViewById(R.id.rl_tab);


        rlTab.setVisibility(View.VISIBLE);
        tv_indicator.setVisibility(View.VISIBLE);
        Drawable drawable = getApplicationContext().getResources().getDrawable(selectorImg[index]);
        tv_indicator.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
        tv_indicator.setText(tabs[index]);


        return inflate;
    }


    private long mExitTime;// 上一次点击返回键的时间

    // 重写onKeyDown方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {  // 是返回键

            LogUtils.e(currentpage);
            if (currentpage.equals("首页")) {

                //如果当前显示的fragment是首页
                // 点击两次返回键退出程序
                if (System.currentTimeMillis() - mExitTime > 2000) {
                    // 如果当前点击返回键的时间减去上一次点击返回键的时间大于2秒
                    ToastUtils.showShort("再按一次退出");
                    // 重新赋值,方便下次使用
                    mExitTime = System.currentTimeMillis();
                } else {
                    // 如果小于两秒,就直接退出程序
                    //SPUtils.getInstance().put(Constant.ISSTARTMIAN, true);
                    //  AppUtils.exitApp();
                    moveTaskToBack(true);

                }
            } else {
                //如果当前不是首页,先切换到首页
                tabHost.setCurrentTab(0);
            }


            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        tabHost.setCurrentTab(0);
    }

}
